package com.example.module_web.view.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.module_web.R;

import java.util.Calendar;


/**
 * Created by SJ on 2018/3/15.
 */

public class AddProduct_Dialog extends Dialog implements View.OnClickListener{
    int count=5;
    EditText[] mEditTexts=new EditText[count];
    TextView mTextView;
    Button mButtonadd,mButtonclean;
    String[] mStrings=new String[6];
    IAddProductData mIAddProductData;
    public AddProduct_Dialog(@NonNull Context context,IAddProductData mIAddProductData) {
        super(context);
        this.mIAddProductData=mIAddProductData;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_dialog_addproductdata);
        for (int i = 0; i <count ; i++) {
            int idd;
            idd =getContext().getResources().getIdentifier("edit_add"+String.valueOf(i), "id", getContext().getPackageName());
            mEditTexts[i] =findViewById(idd);
        }
        mTextView=findViewById(R.id.tv_date);
        mTextView.setOnClickListener(this);
        mButtonadd=findViewById(R.id.btn_add);
        mButtonadd.setOnClickListener(this);
        mButtonclean=findViewById(R.id.btn_clean);
        mButtonclean.setOnClickListener(this);

        Calendar c2 = Calendar.getInstance();
        int year = c2.get(Calendar.YEAR);//年份
        int month = c2.get(Calendar.MONTH) + 1; //月份

        String month2=month<10?"0"+String.valueOf(month):String.valueOf(month);
        int date = c2.get(Calendar.DATE);//日期
        String date2=date<10?"0"+String.valueOf(date):String.valueOf(date);
        mTextView.setText(year+"-"+month2+"-"+date2);
    }

    @Override
    public void onClick(View v) {
        int i1 = v.getId();
        if (i1 == R.id.tv_date) {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dpd2 = new DatePickerDialog(getContext(), Datelistener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            dpd2.show();

        } else if (i1 == R.id.btn_add) {
            mStrings[0] = mTextView.getText().toString();
            for (int i = 0; i < count; i++) {
                if (TextUtils.isEmpty(mEditTexts[i].getText())) {
                    Toast.makeText(getContext(), "部分数据为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    mStrings[i + 1] = mEditTexts[i].getText().toString();
                }
            }
            mIAddProductData.Data(mStrings);

        } else if (i1 == R.id.btn_clean) {
            for (int i = 0; i < count; i++) {
                mEditTexts[i].setText("");
            }

        }
    }
        private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            String month="";
            String day="";
            if (monthOfYear<9){
                month="0"+(monthOfYear+1);
            }else {
                month=""+(monthOfYear+1);
            }
            if (dayOfMonth<10){
                day="0"+dayOfMonth;
            }else {
                day=""+dayOfMonth;
            }
               mTextView.setText(myyear + "-" +month + "-" + day);
        }
    };

}
